import { GameManager, regGameManager } from "../GameManager";
import { LoggerManager } from "./LoggerManager";

/**资源管理器 */
@regGameManager()
export class AssetsManager extends GameManager {

    /**加载并设置图片 */
    static setSprframe(spr: cc.Sprite, path: string, bundleName = "subBundle"): Promise<cc.SpriteFrame> {
        const bundle = bundleName ? cc.assetManager.getBundle(bundleName) : cc.resources
        return new Promise((resolve) => {
            bundle.load(`${path}${''}`, cc.SpriteFrame, (err, sprf: cc.SpriteFrame) => {
                if (err) {
                    LoggerManager.instance().error('setSprframe', path, err)
                    resolve(null)
                    return
                }
                if (!spr.node || !cc.isValid(spr.node, true)) {
                    resolve(sprf)
                    return
                }
                spr.spriteFrame = sprf
                resolve(sprf)
            })
        })

    }

    /**远程加载并设置图片 */
    static setSprframeByRemote(spr: cc.Sprite, path: string): Promise<cc.SpriteFrame> {
        return new Promise((resolve) => {
            cc.assetManager.loadRemote(path, { ext: '.png' }, (err, asset: cc.Texture2D) => {
                if (err) {
                    LoggerManager.instance().error(path, err)
                    resolve(null)
                    return
                }
                asset.packable = false
                const sprf = new cc.SpriteFrame(asset)
                if (!spr.node || !cc.isValid(spr.node, true)) {
                    resolve(sprf)
                    return
                }
                spr.spriteFrame = sprf
                resolve(sprf)
            })
        })
    }

    /**加载并设置spine动画 */
    static setSpineSke(ske: sp.Skeleton, path: string, bundleName = "subBundle") {
        const bundle = bundleName ? cc.assetManager.getBundle(bundleName) : cc.resources

        return new Promise((resolve) => {
            bundle.load(`${path}`, sp.SkeletonData, (err, data: sp.SkeletonData) => {
                if (err) {
                    LoggerManager.instance().error(path, err)
                    resolve(null)
                    return
                }
                if (!ske.node || !cc.isValid(ske.node, true)) {
                    resolve(data)
                    return
                }
                ske.skeletonData = data
                resolve(data)
            })
        })
    }

    /**动态加载预制体并创建实例 */
    static inistaniateByPath(path: string, cbk?, bundleName = "subBundle") {
        const bundle = bundleName ? cc.assetManager.getBundle(bundleName) : cc.resources

        return new Promise<cc.Node>((resolve, reject) => {
            bundle.load(path, cc.Prefab, (err, prefab: cc.Prefab) => {
                if (err) {
                    LoggerManager.instance().log(err);
                    resolve(null)
                    return
                }
                cbk && cbk(prefab)
                const node = cc.instantiate(prefab)
                resolve(node)
            })
        })
    }

    /**
     * 预加载bundle内的预置体
     * @param path 
     * @param bundleName 
     */
    static preloadPrefabByBundle(path: string, bundleName = "subBundle") {
        const bundle = bundleName ? cc.assetManager.getBundle(bundleName) : cc.resources
        if (bundle) {
            bundle.preload(path)
        } else {
            const cdnHost = window["cdn_host"];
            const url = (cdnHost ? cdnHost + "assets/" : "") + bundleName
            cc.assetManager.loadBundle(url, (err: Error, bundle: cc.AssetManager.Bundle) => {
                if (err) {
                    LoggerManager.instance().log(err);
                } else {
                    bundle.preload(path)
                }
            })
        }
    }
}